/**
 * 
 */
package com.iss.umservice.soa;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iss.umservice.soa.impl.ChangePasswordRequest;
import com.iss.umservice.soa.impl.ChangePasswordResponse;
import com.iss.umservice.soa.impl.FindStationUserEntry;
import com.iss.umservice.soa.impl.FindStationUserEntryResponse;
import com.iss.umservice.soa.impl.FindUserEntry;
import com.iss.umservice.soa.impl.FindUserEntryResponse;
import com.iss.umservice.soa.impl.FindValidDataPermittes;
import com.iss.umservice.soa.impl.FindValidDataPermittesResponse;
import com.iss.umservice.soa.impl.FindValidMenuId;
import com.iss.umservice.soa.impl.FindValidMenuIdResponse;
import com.iss.umservice.soa.impl.FindValidOperPermittes;
import com.iss.umservice.soa.impl.FindValidOperPermittesResponse;
import com.iss.umservice.soa.impl.FindValidWorkFlowPermittes;
import com.iss.umservice.soa.impl.FindValidWorkFlowPermittesResponse;
import com.iss.umservice.soa.impl.Login;
import com.iss.umservice.soa.impl.LoginResponse;
import com.iss.umservice.soa.impl.UMPermitteServiceSkeleton;

/**
 * UM Axis2 �����ṩ�ӿڰ�װ��
 * 
 * @author songzou
 * @date 2012-8-8
 */
public class UMPermitteServiceAxis2Proxy extends UMPermitteServiceSkeleton {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private UMPermitteService umPermitteService;

	/**
	 * {@inheritDoc}
	 */
	public LoginResponse login(Login login) {
		String userCode = login.getArgs0();
		String userPassword = login.getArgs1();

		if (logger.isDebugEnabled()) {
			logger.debug("UM soa called [Login] Args=UserCode[{}]", userCode);
		}

		UMLoginResponse returnValue = umPermitteService.login(userCode,
				userPassword);

		com.iss.umservice.soa.impl.xsd.UMLoginResponse loginResponse = new com.iss.umservice.soa.impl.xsd.UMLoginResponse();
		loginResponse.setResponseCode(returnValue.getResponseCode());
		loginResponse.setResponseMessage(returnValue.getResponseMessage());
		loginResponse.setUserIdentifier(returnValue.getUserIdentifier());

		if (logger.isDebugEnabled()) {
			String msg = "UM soa called [Login] Return=Code[{}],Msg[{}],UID[{}]";
			logger.debug(msg, new Object[] { returnValue.getResponseCode(),
					returnValue.getResponseMessage(),
					returnValue.getUserIdentifier() });
		}

		LoginResponse response = new LoginResponse();
		response.set_return(loginResponse);
		return response;
	}

	/**
	 * {@inheritDoc}
	 */
	public FindUserEntryResponse findUserEntry(FindUserEntry findUserEntry) {
		String userIdentifier = findUserEntry.getArgs0();

		if (logger.isDebugEnabled()) {
			logger.debug(
					"UM soa called [findUserEntry] Args=UserIdentifier[{}]",
					userIdentifier);
		}

		UMUserEntryResponse returnValue = umPermitteService
				.findUserEntry(userIdentifier);

		com.iss.umservice.soa.impl.xsd.UMUserEntryResponse entryResponse = new com.iss.umservice.soa.impl.xsd.UMUserEntryResponse();
		entryResponse.setResponseCode(returnValue.getResponseCode());
		entryResponse.setResponseMessage(returnValue.getResponseMessage());
		entryResponse.setUserIdentifier(returnValue.getUserIdentifier());
		entryResponse.setUserName(returnValue.getUserName());

		TLV[] tlvs = returnValue.getTlv();
		if (tlvs == null || tlvs.length == 0) {
			entryResponse.setTlv(new com.iss.umservice.soa.impl.xsd.TLV[0]);
		} else {
			com.iss.umservice.soa.impl.xsd.TLV[] axistlvs = new com.iss.umservice.soa.impl.xsd.TLV[tlvs.length];
			for (int i = 0; i < axistlvs.length; i++) {
				axistlvs[i] = new com.iss.umservice.soa.impl.xsd.TLV();
				axistlvs[i].setKey(tlvs[i].getKey());
				axistlvs[i].setValue(tlvs[i].getValue());
			}
			entryResponse.setTlv(axistlvs);
		}

		if (logger.isDebugEnabled()) {
			String msg = "UM soa called [findUserEntry] Return=Code[{}],Msg[{}],UID[{}],UName[{}]";
			logger.debug(msg,
					new Object[] { returnValue.getResponseCode(),
							returnValue.getResponseMessage(),
							returnValue.getUserIdentifier(),
							returnValue.getUserName() });
		}

		FindUserEntryResponse resonpse = new FindUserEntryResponse();
		resonpse.set_return(entryResponse);
		return resonpse;
	}

	public FindStationUserEntryResponse findStationUserEntry(
			FindStationUserEntry findStationUserEntry) {
		String stationId = findStationUserEntry.getStationId();

		if (logger.isDebugEnabled()) {
			logger.debug(
					"UM soa called [findStationUserEntry] Args=stationId[{}]",
					stationId);
		}
		FindStationUserEntryResponse response = new FindStationUserEntryResponse();
		UMUserEntryResponse[] umUserEntryResponse = umPermitteService
				.findStationUserEntry(findStationUserEntry.getStationId(),
						findStationUserEntry.getDeptId());
		if (umUserEntryResponse == null || umUserEntryResponse.length == 0) {
			return null;
		}
		com.iss.umservice.soa.impl.xsd.UMUserEntryResponse[] entryResponses = new com.iss.umservice.soa.impl.xsd.UMUserEntryResponse[umUserEntryResponse.length];
		int index = 0;
		for (UMUserEntryResponse returnValue : umUserEntryResponse) {
			com.iss.umservice.soa.impl.xsd.UMUserEntryResponse entryResponse = new com.iss.umservice.soa.impl.xsd.UMUserEntryResponse();
			entryResponse.setResponseCode(returnValue.getResponseCode());
			entryResponse.setResponseMessage(returnValue.getResponseMessage());
			entryResponse.setUserIdentifier(returnValue.getUserIdentifier());
			entryResponse.setUserName(returnValue.getUserName());

			TLV[] tlvs = returnValue.getTlv();
			if (tlvs == null || tlvs.length == 0) {
				entryResponse.setTlv(new com.iss.umservice.soa.impl.xsd.TLV[0]);
			} else {
				com.iss.umservice.soa.impl.xsd.TLV[] axistlvs = new com.iss.umservice.soa.impl.xsd.TLV[tlvs.length];
				for (int i = 0; i < axistlvs.length; i++) {
					axistlvs[i] = new com.iss.umservice.soa.impl.xsd.TLV();
					axistlvs[i].setKey(tlvs[i].getKey());
					axistlvs[i].setValue(tlvs[i].getValue());
				}
				entryResponse.setTlv(axistlvs);
			}

			if (logger.isDebugEnabled()) {
				String msg = "UM soa called [findStationUserEntry] Return=Code[{}],Msg[{}],UID[{}],UName[{}]";
				logger.debug(msg, new Object[] { returnValue.getResponseCode(),
						returnValue.getResponseMessage(),
						returnValue.getUserIdentifier(),
						returnValue.getUserName() });
			}
			entryResponses[index] = entryResponse;
			index++;
		}
		response.set_return(entryResponses);
		return response;
	}

	public ChangePasswordResponse changePassword(
			ChangePasswordRequest changePasswordRequest) {

		if (logger.isDebugEnabled()) {
			logger.debug("UM soa called [changePassword] Args:accountCode="
					+ changePasswordRequest.getAccountCode());
		}
		ChangePasswordResponse response = new ChangePasswordResponse();
		response = umPermitteService
				.changePassword(changePasswordRequest.getAccountCode(),changePasswordRequest.getOldPassword(),changePasswordRequest.getNewPassword());
		return response;
	}

	/**
	 * {@inheritDoc}
	 */
	public FindValidMenuIdResponse findValidMenuId(
			FindValidMenuId findValidMenuId) {
		String userIdentifier = findValidMenuId.getArgs0();
		String systemSign = findValidMenuId.getArgs1();

		if (logger.isDebugEnabled()) {
			logger
					.debug(
							"UM soa called [findValidMenuId] Args=UserIdentifier[{}],SystemSign=[{}]",
							userIdentifier, systemSign);
		}

		ValidMenuResponse returnValue = umPermitteService.findValidMenuId(
				userIdentifier, systemSign);

		com.iss.umservice.soa.impl.xsd.ValidMenuResponse menuResponse = new com.iss.umservice.soa.impl.xsd.ValidMenuResponse();
		menuResponse.setResponseCode(returnValue.getResponseCode());
		menuResponse.setResponseMessage(returnValue.getResponseMessage());
		menuResponse.setMenuIds(returnValue.getMenuIds());

		if (logger.isDebugEnabled()) {
			String msg = "UM soa called [findValidMenuId] Return=Code[{}],Msg[{}],MenuId[{}]";
			Object[] args = new Object[] { returnValue.getResponseCode(),
					returnValue.getResponseMessage(),
					Arrays.toString(returnValue.getMenuIds()) };
			logger.debug(msg, args);
		}

		FindValidMenuIdResponse response = new FindValidMenuIdResponse();
		response.set_return(menuResponse);
		return response;
	}

	/**
	 * {@inheritDoc}
	 */
	public FindValidDataPermittesResponse findValidDataPermittes(
			FindValidDataPermittes findValidDataPermittes) {
		String userIdentifier = findValidDataPermittes.getArgs0();
		String systemSign = findValidDataPermittes.getArgs1();

		if (logger.isDebugEnabled()) {
			logger
					.debug(
							"UM soa called [findValidDataPermittes] Args=UserIdentifier[{}],SystemSign=[{}]",
							userIdentifier, systemSign);
		}

		ValidDataPermittesResponse returnValue = umPermitteService
				.findValidDataPermittes(userIdentifier, systemSign);

		com.iss.umservice.soa.impl.xsd.ValidDataPermittesResponse dataPermResponse = new com.iss.umservice.soa.impl.xsd.ValidDataPermittesResponse();
		dataPermResponse.setResponseCode(returnValue.getResponseCode());
		dataPermResponse.setResponseMessage(returnValue.getResponseMessage());
		dataPermResponse.setDataPermittes(returnValue.getDataPermittes());

		if (logger.isDebugEnabled()) {
			String msg = "UM soa called [findValidDataPermittes] Return=Code[{}],Msg[{}],DataPerms[{}]";
			Object[] args = new Object[] { returnValue.getResponseCode(),
					returnValue.getResponseMessage(),
					Arrays.toString(returnValue.getDataPermittes()) };
			logger.debug(msg, args);
		}

		FindValidDataPermittesResponse response = new FindValidDataPermittesResponse();
		response.set_return(dataPermResponse);
		return response;
	}

	/**
	 * {@inheritDoc}
	 */
	public FindValidOperPermittesResponse findValidOperPermittes(
			FindValidOperPermittes findValidOperPermittes) {
		String userIdentifier = findValidOperPermittes.getArgs0();
		String systemSign = findValidOperPermittes.getArgs1();

		if (logger.isDebugEnabled()) {
			logger
					.debug(
							"UM soa called [findValidOperPermittes] Args=UserIdentifier[{}],SystemSign=[{}]",
							userIdentifier, systemSign);
		}

		ValidOperPermittesResponse returnValue = umPermitteService
				.findValidOperPermittes(userIdentifier, systemSign);

		com.iss.umservice.soa.impl.xsd.ValidOperPermittesResponse operPermResponse = new com.iss.umservice.soa.impl.xsd.ValidOperPermittesResponse();
		operPermResponse.setResponseCode(returnValue.getResponseCode());
		operPermResponse.setResponseMessage(returnValue.getResponseMessage());
		operPermResponse.setOperPermittes(returnValue.getOperPermittes());

		if (logger.isDebugEnabled()) {
			String msg = "UM soa called [findValidOperPermittes] Return=Code[{}],Msg[{}],OperPerms[{}]";
			Object[] args = new Object[] { returnValue.getResponseCode(),
					returnValue.getResponseMessage(),
					Arrays.toString(returnValue.getOperPermittes()) };
			logger.debug(msg, args);
		}

		FindValidOperPermittesResponse response = new FindValidOperPermittesResponse();
		response.set_return(operPermResponse);
		return response;
	}

	/**
	 * {@inheritDoc}
	 */
	public FindValidWorkFlowPermittesResponse findValidWorkFlowPermittes(
			FindValidWorkFlowPermittes findValidWorkFlowPermittes) {
		String userIdentifier = findValidWorkFlowPermittes.getArgs0();

		if (logger.isDebugEnabled()) {
			logger
					.debug(
							"UM soa called [findValidWorkFlowPermittes] Args=UserIdentifier[{}]",
							userIdentifier);
		}

		ValidWorkFlowPermittesResponse returnValue = umPermitteService
				.findValidWorkFlowPermittes(userIdentifier);

		com.iss.umservice.soa.impl.xsd.ValidWorkFlowPermittesResponse workFlowPermittesResponse = new com.iss.umservice.soa.impl.xsd.ValidWorkFlowPermittesResponse();
		workFlowPermittesResponse
				.setResponseCode(returnValue.getResponseCode());
		workFlowPermittesResponse.setResponseMessage(returnValue
				.getResponseMessage());
		workFlowPermittesResponse.setWorkFlowPermittes(returnValue
				.getWorkFlowPermittes());

		if (logger.isDebugEnabled()) {
			String msg = "UM soa called [findValidWorkFlowPermittes] Return=Code[{}],Msg[{}],WorkflowPerms[{}]";
			Object[] args = new Object[] { returnValue.getResponseCode(),
					returnValue.getResponseMessage(),
					Arrays.toString(returnValue.getWorkFlowPermittes()) };
			logger.debug(msg, args);
		}

		FindValidWorkFlowPermittesResponse response = new FindValidWorkFlowPermittesResponse();
		response.set_return(workFlowPermittesResponse);
		return response;
	}

	/**
	 * @param umPermitteService the umPermitteService to set
	 */
	public void setUmPermitteService(UMPermitteService umPermitteService) {
		this.umPermitteService = umPermitteService;
	}
}
